// ZhusuPage.cpp : implementation file
//

#include "StdAfx.h"
#include "../Frame/GYWalkAidApp.h"
#include "ZhusuPage.h"
#include "PatMan.h"
#include "../Common/CommonFun.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CZhusuPage dialog


CZhusuPage::CZhusuPage(CWnd* pParent /*=NULL*/)
	: CPageDialog(CZhusuPage::IDD, pParent)
{
	//{{AFX_DATA_INIT(CZhusuPage)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CZhusuPage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CZhusuPage)
	DDX_Control(pDX, IDB_ZHUSU_SAVE, m_btnZhusuSave);
	DDX_Control(pDX, IDC_EDIT_ZHUSU, m_edtZhusu);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CZhusuPage, CDialog)
	//{{AFX_MSG_MAP(CZhusuPage)
	ON_BN_CLICKED(IDB_ZHUSU_SAVE, OnZhusuSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CZhusuPage message handlers

void CZhusuPage::OnZhusuSave() 
{
	if(GYMsgBox(IDS_PAT_CONFIRM_SAVE, MB_YESNO|MB_ICONQUESTION) != IDYES)
		return;

	CPatMan* pParent = (CPatMan*)GetParent();
	if(pParent)
	{
		if(!pParent->SavePatMedInfo())
			return;

		CString strMRN = GetApp()->GetCurMRN();
		if(!SaveData(strMRN))
			return;

		pParent->EnableTab(TAB_INDEX_NOWHIST, TRUE);
		pParent->SelectTab(TAB_INDEX_NOWHIST);

		//  set the focus
		CPageDialog* pPage = pParent->GetTabPage(TAB_INDEX_NOWHIST);
		pPage->EnableInputControls(TRUE);
 		CWnd* pFocus = pPage->GetFocus();
		if(!pFocus || pFocus->GetParent() != pPage)
		{
			//  Get first tab stop control
			CWnd* p1stEdit = pPage->GetNextDlgTabItem(NULL);
			if(p1stEdit)
				p1stEdit->SetFocus();
		}
	}
}

BOOL CZhusuPage::SaveData(CString strMRN)
{
	CString strSQL;
	strSQL.Format(_T("SELECT MedicalRecordNo FROM PatMedInfoText WHERE MedicalRecordNo='%s' AND InfoType=%d"), strMRN, MedicalInfoType);

	CGYDataSet tblInfoText(strSQL);
	if(!tblInfoText.IsEOF())
		return TRUE;

	CTime curTime = CTime::GetCurrentTime();
	CString strCurTime;
	strCurTime.Format(_T("%4d-%02d-%02d"), curTime.GetYear(), curTime.GetMonth(), curTime.GetDay());
	
	CString strText = m_edtZhusu.GetControlData(FALSE);
	strSQL.Format(_T("INSERT INTO PatMedInfoText (MedicalRecordNo, InfoDate, InfoType, InfoText) VALUES ('%s', '%s', %d, '%s')"), strMRN, strCurTime, MedicalInfoType, strText);
	if(!tblInfoText.ExecuteSQL(strSQL))
	{
		DataErrorMsgBox(__FILE__, __LINE__, strSQL, tblInfoText.GetLastErrorString());
		return FALSE;
	}

	strSQL.Format(_T("UPDATE PatientInfo SET DiagProgress=%d WHERE MedicalRecordNo='%s'"), TAB_INDEX_ZHUSU, strMRN);
	tblInfoText.ExecuteSQL(strSQL);

	EnableInputControls(FALSE);
	return TRUE;
}

BOOL CZhusuPage::ShowTableData(CString strMRN)
{
	CString strSQL;
	strSQL.Format(_T("SELECT InfoText FROM PatMedInfoText WHERE MedicalRecordNo='%s' AND InfoType=%d"), strMRN, MedicalInfoType);
	CGYDataSet tblInfoText;
	tblInfoText.OpenTable(strSQL);
	if(tblInfoText.IsEOF())
	{
		m_btnZhusuSave.EnableWindow(TRUE);
		m_edtZhusu.SetWindowText(_T(""));
		return TRUE;
	}

	CString strText;
	tblInfoText.GetFieldValue(_T("InfoText"), strText);
	
	m_edtZhusu.SetWindowText(strText);
	m_btnZhusuSave.EnableWindow(FALSE);
	
	return TRUE;
}

BOOL CZhusuPage::ClearData()
{
	m_edtZhusu.SetWindowText(_T(""));

	return TRUE;
}

BOOL CZhusuPage::EnableInputControls(BOOL bEnable)
{
	m_btnZhusuSave.EnableWindow(bEnable);

	m_edtZhusu.EnableWindow(bEnable);
	m_edtZhusu.SetReadOnly(!bEnable);

	CPageDialog::EnableInputControls(bEnable);
	return TRUE;
}